var phone;
var fo;

function allowCalls(show) 
{
    if(show)
    {
        document.getElementById("waitBlock").style.display = "none"; 
        document.getElementById("callingBlock").style.visibility = "visible";
    }
    else
    {
        document.getElementById("waitBlock").style.display = "inline";
        document.getElementById("callingBlock").style.visibility = "hidden";
    }
}

function updateCallingState(isCalling)
{
    if(isCalling)
    {
        document.getElementById("callBtn").value = document.getElementById("txtHangup").value;
        document.getElementById("callBtn").className = "endcall";
    }
    else
    {
        document.getElementById("callBtn").value = document.getElementById("txtCall").value;
        document.getElementById("callBtn").className = "call";
    }
}

function CreditRefresh(newVal) {
    $("lblCredit").text(newVal);
}

function registrationSucess(msg) {
    //display("registrationSucess " + msg);
}

function registrationFailure(msg) {
    //display("registrationFailure " + msg);
}

function phoneError(msg) {
    //!!!CONECTION ERROR!!!
    display("phoneError " + msg);
    allowCalls(false);
    document.getElementById("flashError").style.display = "inline";
    showConectingMsg(false);
}

function display(msg) {
  try
  {
    console.log(msg);
  }
  catch(e)
  {}
}

function showMicMuted(show)
{
    if(show)
    {
        document.getElementById("micMuted").style.display = "inline";
        showPhone();
    }
    else
    {
        document.getElementById("micMuted").style.display = "none";
        var overlay = $("#fancybox-overlay");
        overlay.hide();
        $("#phoneWrap").css("width", "1px");
        $("#phoneWrap").css("height", "1px");
        $("#phoneWrap").css("padding", "0px");
    }
}

function setupApp() {
    fo = new SWFObject("/Scripts/bin-release/Red5Phone2.swf", "lzapp", $(window).width() * (90 / 100), $(window).height() * (90 / 100), "8");
    fo.addParam("swLiveConnect", "true");
    fo.addParam("name", "lzapp");
    fo.addParam("id", "lzapp");
    fo.write("phoneapi");

    phone = document.getElementById("lzapp");
    if (phone == null) {
        alert("Can't initialize dialer object");
    }

    $("#phoneWrap").css("position", "absolute");
    $("#phoneWrap").css("top", ($(window).height()*(9/100)) / 2 + $(window).scrollTop() + "px");
    $("#phoneWrap").css("left", ($(window).width() * (9 / 100)) / 2 + $(window).scrollLeft() + "px");
}

function flashLoaded()
{
    //setTimeout("register();", 150);
    allowCalls(true);
}

function register() {
    var peer = document.getElementById("peer").value;
    phone.login("", peer, peer, peer, "pass", "", "10.13.0.152", "rtmp://10.13.0.152/sip", "", document.getElementById("ext").value, 7000);
    //phone.call(document.getElementById("code").value);
}

function showConectingMsg(show)
{
    if(show)
    {
        document.getElementById("waitBlock").style.display = "inline";
        document.getElementById("waitBlock").innerHTML = "Stabilire conexiune... <br/>";
    }
    else
    {
        document.getElementById("waitBlock").style.display = "none"; 
    }
}

function showPhone() {
    $("#phoneWrap").css("width", "auto");
    $("#phoneWrap").css("height", "auto");
    $("#phoneWrap").css("padding", "10px");

    var overlay = $("#fancybox-overlay");
    overlay.css({
        'background-color': '#777',
        'opacity': 0.7,
        'height': $(document).height()
    });
    overlay.show();
}

$(document).ready(function () {
    setupApp();
});

function onClick() {
    $('#tabs').tabs('select', "#tab-tel");
    if (document.getElementById("callBtn").value == "Apeleaza") {
        PageMethods.RequestCall(document.getElementById('phoneNumber').value, OnSuccess, OnFailure);
    }
    else {
        document.getElementById("callBtn").value = "Apeleaza";
        document.getElementById("callBtn").className = "call";
        phone.hangup();
        showConectingMsg(false);

        //Update credit
        PageMethods.LoadUserCredit(CreditRefresh, OnFailure);
    }
}

function OnSuccess(result) {
    var json = eval("(" + result + ")");
    document.getElementById("ext").value = json.ext;
    document.getElementById("peer").value = json.peer;
    if (json.Code == "200") {
        document.getElementById("callBtn").value = "Inchide";
        document.getElementById("callBtn").className = "endcall";
        register();
    }
    else {
        //alert("Code = " + json.Code);
        alert(json.Message);
    }
}

function OnFailure(error) {
    for (var name in error) {
        if (error.hasOwnProperty(name)) {
            document.getElementById("callstatus").innerHTML += name + " : " + error[name] + "<br />";
        }
    }

}